// ReportTempletDataQueryHandler.cpp: implementation of the ReportTempletDataQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "ReportTempletDataQueryHandler.h"
#include "DatabaseManager.h"
#include "ReportTempletSet.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ReportTempletDataQueryHandler::ReportTempletDataQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

ReportTempletDataQueryHandler::~ReportTempletDataQueryHandler()
{
}

int ReportTempletDataQueryHandler::Handling()
{
	ReportTempletQueryStruct * pReportTempletQueryStruct = (ReportTempletQueryStruct * )m_pPacket->GetpData();
	ReportTempletAckStruct * pReportTempletAckStruct = QueryReportTempletData(pReportTempletQueryStruct->m_dwMethodType, pReportTempletQueryStruct->m_szUserId, pReportTempletQueryStruct->m_szTempletName);

	Packet packetAck(pReportTempletAckStruct->m_dwBufSize, (BYTE *)pReportTempletAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pReportTempletAckStruct;

	return 0;
}

ReportTempletAckStruct * ReportTempletDataQueryHandler::QueryReportTempletData(DWORD dwMethodType, const CString& strUserId, const CString& strTempletName)
{
	/*
	CString	m_CreateDate;
	CString	m_CreateTime;
	CByteArray	m_Data;
	BYTE	m_ShareFlag;
	CString	m_TempletName;
	CString	m_UserId;
	m_CreateDate = _T("");
	m_CreateTime = _T("");
	m_ShareFlag = 0;
	m_TempletName = _T("");
	m_UserId = _T("");

	CAdoRecordSet  m_adoRecordSet;
	CAdoConnection *m_adopConnection;
	CString	m_strSQL;

	ReportTempletAckStruct * pReportTempletAckStruct;

	m_adoRecordSet.SetAdoConnection(m_adopConnection);
	m_adoRecordSet.SetCursorLocation();

	m_strSQL.Format("SELECT *, UserId, TempletName, CreateDate, CreateTime, ShareFlag, Data FROM ReportTemplet where UserId = '%s' AND TempletName = '%s'", strUserId, strTempletName);
	m_adoRecordSet.Open(m_strSQL, adCmdText);
	if (m_adoRecordSet.IsEOF())
	{
	pReportTempletAckStruct = new ReportTempletAckStruct;
	pReportTempletAckStruct->m_nMethodAck = VCS_MR_TEMPLETNOTEXIST;
	pReportTempletAckStruct->m_dwBufSize = sizeof(ReportTempletAckStruct);
	pReportTempletAckStruct->m_dwNumOfItems = 0;
	}
	else
	{
	m_adoRecordSet.GetCollect("UserId",m_UserId);
	m_adoRecordSet.GetCollect("TempletName",m_TempletName);
	m_adoRecordSet.GetCollect("CreateDate",m_CreateDate);
	m_adoRecordSet.GetCollect("CreateTime",m_CreateTime);
	m_adoRecordSet.GetCollect("ShareFlag",m_ShareFlag);
	m_adoRecordSet.GetCollect("Data",m_Data);
	int nDataSize = m_Data.GetSize()-1;
	int nBufSize = sizeof(ReportTempletAckStruct)+nDataSize-1;
	pReportTempletAckStruct = (ReportTempletAckStruct *) new BYTE[nBufSize];
	pReportTempletAckStruct->m_nMethodAck = VCS_MR_OK;
	pReportTempletAckStruct->m_dwBufSize = nBufSize;
	pReportTempletAckStruct->m_dwNumOfItems = 1;
	strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szUserId, m_UserId);
	strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szTempletName, m_TempletName);
	strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szCreateDate, m_CreateDate);
	strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szCreateTime, m_CreateTime);
	pReportTempletAckStruct->m_ReportTempletItems[0].m_nShareFlag = m_ShareFlag;
	pReportTempletAckStruct->m_ReportTempletItems[0].m_dwDataSize = nDataSize;
	for (int i = 0;i < nDataSize;i++)
	{
	pReportTempletAckStruct->m_ReportTempletItems[0].m_byData[i] = m_Data.GetAt(i);
	}
	}
	pReportTempletAckStruct->m_dwMethodType = dwMethodType;
	m_adoRecordSet.Close();

	return pReportTempletAckStruct;*/

	//*
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CReportTempletSet rsReportTempletSet(pDatabase);
	rsReportTempletSet.m_strFilter.Format("UserId = '%s' AND TempletName = '%s'", strUserId, strTempletName);
	rsReportTempletSet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::readOnly);
	ReportTempletAckStruct * pReportTempletAckStruct;
	if (rsReportTempletSet.IsEOF())
	{
		pReportTempletAckStruct = new ReportTempletAckStruct;
		pReportTempletAckStruct->m_nMethodAck = VCS_MR_TEMPLETNOTEXIST;
		pReportTempletAckStruct->m_dwBufSize = sizeof(ReportTempletAckStruct);
		pReportTempletAckStruct->m_dwNumOfItems = 0;
	}
	else
	{
		int nDataSize = rsReportTempletSet.m_Data.GetSize()-1;
		int nBufSize = sizeof(ReportTempletAckStruct)+nDataSize-1;
		pReportTempletAckStruct = (ReportTempletAckStruct *) new BYTE[nBufSize];
		pReportTempletAckStruct->m_nMethodAck = VCS_MR_OK;
		pReportTempletAckStruct->m_dwBufSize = nBufSize;
		pReportTempletAckStruct->m_dwNumOfItems = 1;
		strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szUserId, rsReportTempletSet.m_UserId);
		strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szTempletName, rsReportTempletSet.m_TempletName);
		strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szCreateDate, rsReportTempletSet.m_CreateDate);
		strcpy(pReportTempletAckStruct->m_ReportTempletItems[0].m_szCreateTime, rsReportTempletSet.m_CreateTime);
		pReportTempletAckStruct->m_ReportTempletItems[0].m_nShareFlag = rsReportTempletSet.m_ShareFlag;
		pReportTempletAckStruct->m_ReportTempletItems[0].m_dwDataSize = nDataSize;
		for (int i = 0;i < nDataSize;i++)
		{
			pReportTempletAckStruct->m_ReportTempletItems[0].m_byData[i] = rsReportTempletSet.m_Data.GetAt(i);
		}
	}
	pReportTempletAckStruct->m_dwMethodType = dwMethodType;
	rsReportTempletSet.Close();

	return pReportTempletAckStruct;
	//*/
}